<?php
namespace Zxf5115\Laravel\Keyword\Logics;

use Illuminate\Http\Request;

/**
 * @author zhangxiaofei [<1326336909@qq.com>]
 * @dateTime 2025-10-08
 *
 * 关键字逻辑处理类
 */
class KeywordLogic
{
  /**
   * 关键字数据处理
   *
   * @param Request $request 用户请求参数
   * @param string $modelClass 操作模型类
   * @return void
   */
  public function handle(Request $request, string $modelClass) : void
  {
    $model = $modelClass::firstOrNew(['id' => $request->id]);

    $model->title = $request->title;
    $model->total = $request->total ?? 1;
    $model->status = intval($request->status);
    $model->save();
  }


  /**
   * 关键字记录处理
   *
   * @param Request $request 用户请求参数
   * @param string $modelClass 操作模型类
   * @return void
   */
  public function record(Request $request, string $modelClass) : void
  {
    $model = $modelClass::firstOrNew(['title' => $request->title]);

    $modelClass::updateOrCreate(
      ['title' => $request->title], // 查询条件
      ['status' => 1] // 新增时的默认值
    )->increment('total', $request->total ?? 1); // 无论新增还是存在，都增加total
  }
}
